package com.bjsxt.thread;

public class DaemonThread {
    public static void main(String[] args) {
//        Thread t = new Thread(new Daemon(),"daemon");
//        //设置为守护线程 守护主线程  随着主线程的消亡而消亡
//        t.setDaemon(true);
//        t.start();
//        try {
//            Thread.sleep(1000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        System.out.println("主线程结束");

        Thread t2 = new Thread(new UserThrad(),"UserThread");
        t2.start();
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("主线程结束");


    }
}

class Daemon implements Runnable{
    @Override
    public void run() {
        for (int i = 0;i<20;i++){
            System.out.println(Thread.currentThread().getName()+ " "+i);
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

class UserThrad implements Runnable{
    @Override
    public void run() {
        Thread t = new Thread(new Daemon(),"Daemon");
        t.setDaemon(true);
        t.start();
        for (int i=0;i<5;i++){
            System.out.println(Thread.currentThread().getName()+ " "+i);
            try {
                Thread.sleep(500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}